//
//  AppDelegate.m
//  UI09_UITabBarController
//
//  Created by wyx on 16/3/21.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "RootTabBarController.h"
#import "NaviOfRootFirstTabBarItem.h"
#import "VCOfSecondTabBarItem.h"
#import "VCOf3rdTabBarItem.h"
#import "VCOf4thTabBarItem.h"
#import "VCOf5thTabBarItem.h"
#import "VCOf6thTabBarTiem.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate
-(void)dealloc{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor cyanColor];
    //创建 controller 对象.
    //ViewController *vc = [[ViewController alloc] init];
    //指定 window 跟视图控制器(进入 App 之后看到第一界面)
    //逻辑代码和 UI 基本都是在 controller中编写
    //详见 ViewController.m 文件
    //创建 UITabBar对象
    [self createtabBarController];
    [_window release];
//    [vc release];
    return YES;
}
#pragma mark - 知识点1:UITabBarController
- (void)createtabBarController{
    RootTabBarController *root = [[RootTabBarController alloc] init];
    //设置 root 的 viewController 属性
    root.viewControllers = [self createRootOfVCs];
    //指定代理人
    root.delegate = self;
    self.window.rootViewController = root;
    [root release];
    
}
- (NSArray<UIViewController *> *)createRootOfVCs {
    NSMutableArray *arrOfVCs = [NSMutableArray array];
    //第一个 tabBarItem
    ViewController *vc = [[ViewController alloc] init];
    NaviOfRootFirstTabBarItem *navi = [[NaviOfRootFirstTabBarItem alloc] initWithRootViewController:vc];
    [arrOfVCs addObject:navi];
    [navi release];
    navi.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:100];
    navi.tabBarItem.badgeValue = @"+99";
    
    //第二个 tabBarItem
    VCOfSecondTabBarItem *item2 = [[VCOfSecondTabBarItem alloc] init];
    [arrOfVCs addObject:item2];
    [item2 release];
    item2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"地图" image:[UIImage imageNamed:@"iconfont-ditu-1"] tag:200];
    
    //第三个 tabBarItem
    VCOf3rdTabBarItem *item3 = [[VCOf3rdTabBarItem alloc] init];
    [arrOfVCs addObject:item3];
    [item3 release];
    item3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"联系人" image:[UIImage imageNamed:@"iconfont-crmtubiao32"] tag:300];
    //第四个 tabBarItem
    VCOf4thTabBarItem *item4 = [[VCOf4thTabBarItem alloc] init];
    [arrOfVCs addObject:item4];
    [item4 release];
    item4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Book" image:[UIImage imageNamed:@"iconfont-book"] tag:400];
    //第五个 tabBarItem
    VCOf5thTabBarItem *item5 = [[VCOf5thTabBarItem alloc] init];
    [arrOfVCs addObject:item5];
    [item5 release];
    item5.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"收藏" image:[UIImage imageNamed:@"iconfont-shoucang"] tag:500];
    //第六个 tabBarItem
    VCOf6thTabBarTiem *item6 = [[VCOf6thTabBarTiem alloc] init];
    [arrOfVCs addObject:item6];
    [item6 release];
    item6.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"iconfont-shezhi"] tag:600];
    return arrOfVCs;
}
#pragma mark - 知识点2:UITabBarControllerDelegate
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //选择第一个 Item时,第一个 Item 角标消失 选择哪一个哪一个消失
    viewController.tabBarItem.badgeValue = nil;
    
    
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
